<template>
  <div class="user">
    <div class="form">
      <el-form label-position="right" :inline="true" :model="userForm">
        <el-form-item label="用户名" class="formItem">
          <el-input
            v-model="userForm.name"
            placeholder="请输入用户名"
            clearable
          />
        </el-form-item>
        <el-form-item label="账户" class="formItem">
          <el-input
            v-model="userForm.userName"
            placeholder="请输入账户"
            clearable
          />
        </el-form-item>
        <el-form-item label="角色" class="formItem">
          <el-select v-model="userForm.pos" clearable placeholder="角色">
            <el-option label="普通用户" value="普通用户" />
            <el-option label="管理员" value="管理员" />
          </el-select>
        </el-form-item>
        <el-form-item label="手机" class="formItem">
          <el-input v-model="userForm.phone" placeholder="请输入账户" clearable />
        </el-form-item>
        <el-form-item label="邮箱" class="formItem">
          <el-input v-model="userForm.email" placeholder="请输入账户" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">搜索</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-divider />
    <div v-if="userList.length > 0" class="table">
      <!-- 表格展示 -->
      <el-table
        :data="userList"
        stripe
        style="width: 100%"
        v-loading="loading" 
      >
        <el-table-column prop="id" label="用户id" />
        <el-table-column prop="name" label="用户名" />
        <el-table-column prop="userName" label="账号" />
        <el-table-column label="头像">
          <template #default="scope">
            <el-avatar :src="scope.row.avatar" />
          </template>
        </el-table-column>
        <el-table-column prop="pos" label="角色" />
        <el-table-column prop="phone" label="手机" />
        <el-table-column prop="email" label="邮箱" />
        <el-table-column prop="balance" label="余额" />
        <el-table-column fixed="right" label="操作" width="180">
          <template #default="scope">
            <el-button
              link
              type="primary"
              size="small"
              @click="showRecharge(scope.row.id)"
              >充值</el-button
            >
            <el-button
              link
              type="primary"
              size="small"
              @click="deleteUser(scope.row.id)"
              >删除</el-button
            >
            <el-button
              link
              type="primary"
              size="small"
              @click="setAdmin(scope.row)"
              >设为管理员</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div v-if="userList.length > 0" style="float: right">
      <!-- 分页 -->
      <el-pagination
        v-model:current-page="pageInfo.pageNum"
        v-model:page-size="pageInfo.pageSize"
        :page-sizes="[10, 15, 20]"
        :total="pageInfo.total"
        :disabled="false"
        background
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <!-- 空状态 -->
    <div v-if="userList.length <= 0">
      <el-empty description="暂无数据" />
    </div>
  </div>

  <!-- 充值对话框 -->
  <el-dialog
    v-model="rechargeDialogVisible"
    :title="rechargeUser.name +'-'+ '充值'"
    width="30%"
  >
    <el-input type="number" v-model="rechargeUser.balance" />
    <template #footer>
      <el-button type="primary" @click="rechargeEvent">充值</el-button>
      <el-button type="info" @click="rechargeDialogVisible = false">取消</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { pageList, getUserById,recharge,deleteById, updateById } from "@/api/user.js";
import { onMounted, reactive, ref } from "vue";
import { ElLoading ,ElMessage, ElMessageBox} from 'element-plus'


const userForm = reactive({
  name: "",
  userName: "",
  pos: "",
  phone: "",
  email: "",
});
const userList = ref([]);
const pageInfo = reactive({
  total: 0,
  pageNum: 1,
  pageSize: 10,
});
const loading = ref(false)
const rechargeDialogVisible = ref(false)
onMounted(async () => {
  await search();
});
// 注册用户信息
const rechargeUser = reactive({
  id: -1, // 用户id
  name: "", // 昵称
  avatar: "", // 头像
  balance: 0  // 账户
})

/**
 * 搜索
 */
const search = async () => {
  try{
    // 开启加载
    loading.value = true;
    const response = await pageList(pageInfo, userForm);
    userList.value = response.data.data.list;
    pageInfo.total = response.data.data.total;
  }finally{
    loading.value = false;
  }
};
/**
 * 重置
 */
const reset = () => {
  userForm.userName = "";
  userForm.email = "";
  userForm.name = "";
  userForm.phone = "";
  userForm.pos = "";
  // 重新搜索
  search();
};

/**
 * 页数大小发生改变
 */
const handleSizeChange = async (pageSize)=>{
  console.log('页面大小发生变化');
  pageInfo.pageSize = pageSize;
  await search();
}

const handleCurrentChange = async(pageNum)=>{
  console.log('页码发生了变化!');
  pageInfo.pageNum = pageNum;
  await search();
}
/**
 * 删除用户信息
 */
const deleteUser = async (id) => {
  try{
    await ElMessageBox.confirm(
      '确定要删除该用户吗?',
      '警告',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
    )
    console.log('点击了确定')
    await deleteById(id);
    // 删除后，重新刷新列表
    await search();
  }catch(e) {
    console.log('点击了取消')
  }
  
}
/**
 * 显示充值界面
 */
const showRecharge = async (userId) => {
  console.log('点击了充值按钮');
  console.log(userId);
  // 查询用户信息
  const response = await getUserById(userId);
  const user = response.data.data;
  console.log(response.data.data);
  rechargeUser.id = user.id;
  rechargeUser.name = user.name;
  rechargeUser.avatar = user.avatar;
  rechargeUser.balance = user.balance
  // 显示充值界面
  rechargeDialogVisible.value = true;
};

/**
 * 充值
 */
const rechargeEvent = async()=>{
  // 获取信息,开启全局加载
  const loading = ElLoading.service({ fullscreen: true })
  // 请求后端接口
  try{
    await recharge(rechargeUser.id, rechargeUser.balance);
  }finally{
    loading.close();
    // 关闭弹框
    rechargeDialogVisible.value = false;
    // 重新刷新列表
    await search()
  }
}


/**
 * 设置为管理员
 */
const setAdmin = async (user) => {
  // 修改数据
  user.pos = '管理员';
  // 请求接口
  await updateById(user)
  // 重新刷新表格
  await search()
};
</script>

<style scoped>
.form {
  width: 100%;
  height: 100px;
}
.formItem {
  width: 240px;
}
</style>
